<template>
  <div class="home-container">
    <h2>首页</h2>
    <p>欢迎回来！</p>
    <button @click="testRequest">测试请求</button>
    <button @click="testExpiredToken">测试过期Token</button>
    <button @click="testRequestQueue">测试请求队列</button>
    <button @click="logout">退出登录</button>
    <div class="result-container" v-if="result">
      <h3>请求结果:</h3>
      <pre>{{ result }}</pre>
    </div>
    <div class="queue-info" v-if="queueLength > 0">
      <p>队列中等待的请求数: {{ queueLength }}</p>
    </div>
    <div class="loading" v-if="loading">加载中...</div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import request from '../utils/request';
import { useRouter } from 'vue-router';

const result = ref(null);
const loading = ref(false);
const queueLength = ref(0);
const router = useRouter();

// 监听请求队列长度变化
const updateQueueLength = (length) => {
  queueLength.value = length;
};

// 将更新队列长度的方法添加到window对象，供request.js调用
window.updateQueueLength = updateQueueLength;


// 测试普通请求
const testRequest = async () => {
  loading.value = true;
  try {

    console.log(111111)
    const res = await request.get('/test');
    console.log(3333333)

    result.value = res;
  } catch (err) {
    result.value = { error: err.message };
  } finally {
    loading.value = false;
  }
};

// 测试过期token的情况
const testExpiredToken = async () => {
  // 手动设置一个过期的token
  localStorage.setItem('token', 'expired_token');
  loading.value = true;
  try {
    const res = await request.get('/test');
    result.value = res;
  } catch (err) {
    result.value = { error: err.message };
  } finally {
    loading.value = false;
  }
};

// 测试请求队列
const testRequestQueue = async () => {
  // 手动设置一个过期的token
  // localStorage.setItem('token', 'expired_token');
  loading.value = true;
  result.value = { message: '开始测试请求队列...' };
  queueLength.value = 6; // 初始化为3个请求

  try {
    // 一个登录过期的请求
    testRequest()
    console.log(2222222)

    // 同时发起多个请求
    const promise1 = request.get('/test?request=1');
    const promise2 = request.get('/test?request=2');
    const promise3 = request.get('/test?request=3');

    // 等待所有请求完成
    const [res1, res2, res3] = await Promise.all([promise1, promise2, promise3]);

    result.value = {
      message: '所有请求完成',
      results: [
        { request: 1, data: res1 },
        { request: 2, data: res2 },
        { request: 3, data: res3 },
      ]
    };
  } catch (err) {
    result.value = { error: err.message };
  } finally {
    loading.value = false;
    queueLength.value = 0;
  }
};

// 退出登录
const logout = () => {
  localStorage.removeItem('token');
  localStorage.removeItem('refreshToken');
  router.push('/login');
};
</script>

<style scoped>
.home-container {
  width: 1000px;
  margin: 0 auto;
  padding: 2rem;
}

button {
  margin-right: 1rem;
  padding: 0.5rem 1rem;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.result-container {
  margin-top: 2rem;
  padding: 1rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  background-color: #f8f9fa;
}

.queue-info {
  margin-top: 1rem;
  padding: 0.5rem;
  background-color: #e9ecef;
  border-radius: 4px;
  color: #495057;
}

pre {
  white-space: pre-wrap;
  word-break: break-all;
}

.loading {
  margin-top: 1rem;
  color: #42b983;
  font-weight: bold;
}
</style>